# 完整部分重写脚本，确保正确的缩进和格式
import os
import shutil

# 文件路径
original_file = r'd:\未知\yolo_apply\video_processing\video_processor.py'
final_backup = r'd:\未知\yolo_apply\video_processing\video_processor.py.final_backup'
temp_file = r'd:\未知\yolo_apply\video_processing\video_processor_rewritten.py'

# 确保我们使用最终备份
if os.path.exists(final_backup):
    print(f"使用最终备份文件: {final_backup}")
    
    # 读取文件内容
    with open(final_backup, 'r', encoding='utf-8', errors='replace') as f:
        lines = f.readlines()
    
    # 找到问题区域前后的代码
    start_section = None
    end_section = None
    section_before = []
    section_after = []
    
    # 找到问题区域的位置
    for i, line in enumerate(lines):
        if '# 错误率过高时触发警告和恢复' in line:
            start_section = i
        elif start_section is not None and 'error_count = 0' in line:
            end_section = i
            break
    
    print(f"问题区域从第{start_section+1}行到第{end_section+1}行")
    
    # 获取问题区域前的代码
    section_before = lines[:start_section]
    
    # 确定正确的缩进级别（基于前一行的缩进）
    if start_section > 0:
        prev_line = lines[start_section-1]
        indent_level = len(prev_line) - len(prev_line.lstrip())
        indent = ' ' * indent_level
    else:
        indent = ''
    
    # 重写中间的部分，确保正确的缩进
    middle_section = [
        '# 错误率过高时触发警告和恢复\n',
        indent + 'error_count = 0\n'
    ]
    
    # 获取问题区域后的代码
    section_after = lines[end_section+1:]
    
    # 合并所有部分并写入临时文件
    with open(temp_file, 'w', encoding='utf-8', newline='\n') as f:
        # 写入问题区域前的代码
        f.writelines(section_before)
        # 写入修复后的中间部分
        f.writelines(middle_section)
        # 写入问题区域后的代码
        f.writelines(section_after)
    
    print(f"已创建完全重写的临时文件: {temp_file}")
    
    # 替换原文件
    os.replace(temp_file, original_file)
    print(f"已用重写的文件替换原文件")
    print("现在让我们再次尝试编译...")
else:
    print(f"错误: 找不到最终备份文件 {final_backup}")